interface Product {
  id: number;
  name: string;
  category: number;
  price: number;
  description?: string;
  createdAt: Date;
  updatedAt: Date;
}

// 产品数据存储
let products: Product[] = [];
let nextId = 1;

// 获取所有产品
export function getAllProducts(): Product[] {
  return [...products];
}

// 根据ID获取产品
export function getProductById(id: number): Product | undefined {
  return products.find(p => p.id === id);
}

// 添加新产品
export function addProduct(product: Omit<Product, 'id' | 'createdAt' | 'updatedAt'>): Product {
  const newProduct: Product = {
    ...product,
    id: nextId++,
    createdAt: new Date(),
    updatedAt: new Date()
  };
  products.push(newProduct);
  return newProduct;
}

// 更新产品
export function updateProduct(id: number, updates: Partial<Omit<Product, 'id' | 'createdAt'>>): Product | undefined {
  const index = products.findIndex(p => p.id === id);
  if (index === -1) return undefined;

  const updatedProduct = {
    ...products[index],
    ...updates,
    updatedAt: new Date()
  };
  products[index] = updatedProduct;
  return updatedProduct;
}

// 删除产品
export function deleteProduct(id: number): boolean {
  const index = products.findIndex(p => p.id === id);
  if (index === -1) return false;

  products.splice(index, 1);
  return true;
}